<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>

    </style>
</head>

<body>
    <div class="pwdBox">
        请输入密码：<input type="password"> <i id="eye">🎀</i>
    </div>
</body>
<script>
    var that;
    var eyeflag = false; // false代表眼睛关闭
    class PwdBox {
        constructor(cla) {
            that = this;
            this.main = document.querySelector(cla);
            // 获取元素
            this.ipt = this.main.querySelector('input');
            this.eye = this.main.querySelector('#eye');
            // 绑定事件
            this.init();
        };
        // 绑定事件
        init() {
            // 眼睛的点击事件
            this.eye.onclick = this.toggleEye;
            this.eye.onselectstart = this.banSelect;
        };
        // 切换眼睛状态
        toggleEye() {
            // 眼睛默认首次打开页面时是关闭状态
            // 使用flag标记眼睛此时的状态
            // var flag = false; // false代表眼睛关闭
            if (eyeflag == false) {
                this.innerHTML = '👁';
                that.toggleIptType();
                eyeflag = true;
            } else {
                this.innerHTML = '🎀';
                that.toggleIptType();
                eyeflag = false;
            }

        };
        // 切换表单类型
        toggleIptType() {
            if (eyeflag == false) {
                this.ipt.type = 'text';
            } else {
                this.ipt.type = 'password';
            }
            
        };
        // 禁止選中
        banSelect(e) {
            // this.onselectstart = function(e) {
            //     e.preventDefault();
            // }
            e.preventDefault();
        };
    }
    new PwdBox('.pwdBox')
</script>


</html>